package com.marvin.demo.conf;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;

/**
 * 精诚所至，金石为开。
 * 石の上にも三年;陽気の発する所金石亦透る。
 *
 * @Author mahaiqiang
 * @Create 2020/2/5 10:17 上午
 * @Desc TODO
 **/
@Slf4j
public class DesCbcUtil {
    // 加解密统一使用的编码方式
    private final static String encoding = "UTF-8";

    /**
     * 3DES加密
     *
     * @param plainText 普通文本
     * @return 加密后的文本，失败返回null
     */
    public static String encode(String plainText, String secretKey, String iv) {
        String result = null;
        try {
            DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("desede");
            Key desKey = secretKeyFactory.generateSecret(deSedeKeySpec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, desKey, ips);
            byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
            result = Base64Utils.encodeToString(encryptData);
        } catch (Exception e) {
            log.error("DesCbcUtil encode error : {}", e);
        }
        return result;
    }

    /**
     * 3DES解密
     *
     * @param encryptText 加密文本
     * @return 解密后明文，失败返回null
     */
    public static String decode(String encryptText, String secretKey, String iv) {
        String result = null;
        try {
            DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("desede");
            Key desKey = secretKeyFactory.generateSecret(spec);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, desKey, ips);
            byte[] decryptData = cipher.doFinal(Base64Utils.decodeFromString(encryptText));
            result = new String(decryptData, encoding);
        } catch (Exception e) {
            log.error("DesCbcUtil decode error : {}", e.getMessage());
        }
        return result;
    }

    public static void main(String[] args) {
        String secretKey = "b2c17b46e2b1415392aab5a82869856c";
        String iv = "61960842";
//        String body = "{\"name\":\"小强\"}";
        String body = "{\"code\":0,\"msg\":\"操作成功\",\"data\":[{\"part_intro\":null,\"part_id\":\"50001\",\"part_name\":\"胸围(成)\",\"part_name_en\":null,\"required_flag\":1,\"image_url\":null,\"min_size\":75,\"max_size\":180,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50002\",\"part_name\":\"中腰(成)\",\"part_name_en\":null,\"required_flag\":1,\"image_url\":null,\"min_size\":60,\"max_size\":170,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50003\",\"part_name\":\"臀围(成)\",\"part_name_en\":null,\"required_flag\":1,\"image_url\":null,\"min_size\":70,\"max_size\":190,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50004\",\"part_name\":\"袖肥一圈(成)\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":25,\"max_size\":70,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50005\",\"part_name\":\"袖口一圈(成)\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":20,\"max_size\":50,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50006\",\"part_name\":\"总肩宽(成)\",\"part_name_en\":null,\"required_flag\":1,\"image_url\":null,\"min_size\":30,\"max_size\":65,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50007\",\"part_name\":\"后背宽(成)\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":null,\"max_size\":null,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50008\",\"part_name\":\"左袖长(成)\",\"part_name_en\":null,\"required_flag\":1,\"image_url\":null,\"min_size\":15,\"max_size\":90,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50009\",\"part_name\":\"右袖长(成)\",\"part_name_en\":null,\"required_flag\":1,\"image_url\":null,\"min_size\":15,\"max_size\":90,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50010\",\"part_name\":\"后衣长(成)\",\"part_name_en\":null,\"required_flag\":1,\"image_url\":null,\"min_size\":45,\"max_size\":125,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50011\",\"part_name\":\"前衣长(成)\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":null,\"max_size\":null,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50012\",\"part_name\":\"颈围(净)\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":30,\"max_size\":65,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50013\",\"part_name\":\"上臂围(净)\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":20,\"max_size\":70,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50014\",\"part_name\":\"后腰节长\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":null,\"max_size\":null,\"part_code\":null},{\"part_intro\":null,\"part_id\":\"50015\",\"part_name\":\"首扣距离\",\"part_name_en\":null,\"required_flag\":0,\"image_url\":null,\"min_size\":null,\"max_size\":null,\"part_code\":null}]}";
        String secretBody = encode(body, secretKey, iv);
        System.out.println(secretBody);
    }
}
